package ca.jerg.data;

import java.io.*;

public class ErgFileHeader implements IErgPoint
{
	public static final String HEADER = "[COURSE HEADER]";
	public static final String FOOTER = "[END COURSE HEADER]";
	
	public static void main( String args[] ) throws IOException
	{
		ErgFileHeader efh = new ErgFileHeader();
		BufferedReader br = new BufferedReader(new FileReader("at-test.erg"));
		System.out.println(efh.read(br));
		efh.write();
	}
	
	private String version;
	private String units;
	private String description;
	private String fileName;
	private final String columns[] = {"MINUTES","WATTS"};
	
	/**
	 * Create a blank .ERG file header.  Version is initialized
	 * to 2 and units default to metric.  All other fields are null.
	 */
	public ErgFileHeader()
	{
		version = "VERSION = 2";
		units = "UNITS = METRIC";
	}
	/**
	 * Create a full operational .ERG file header.
	 * 
	 * @param version		the version of the .ERG file
	 * @param units			the units used in the .ERG file
	 * @param description	a text description of the workout specified by the .ERG file
	 * @param fileName		the file name of the .Erg file
	 */
	public ErgFileHeader( String version, String units, String description, String fileName )
	{
		this.version = "VERSION = " + version;
		this.units = "UNITS = " + units;
		this.description = "DESCRIPTION = " + description;
		this.fileName = "FILE NAME = " + fileName;
	}
	
	/**
	 * Returns the .ERG file's description field
	 */
	public String getDescription()
	{
		return description;
	}
	
	/**
	 * Returns the .ERG file's file name
	 */
	public String getFileName()
	{
		return fileName;
	}
	
	/**
	 * Returns the .ERG file's unit selection
	 */
	public String getUnits()
	{
		return units;
	}

	/**
	 * Returns the .ERG file's revision
	 */
	public String getVersion()
	{
		return version;
	}

	//@Override
	public int read( BufferedReader in )
	{
		try
		{
			// 1 - [COURSE HEADER]
			if ( in.readLine().equals("[COURSE HEADER]") == false )
				return 2;
			
			// 2 - VERSION = x
			version = in.readLine();
			
			// 3 - UNITS = str
			units = in.readLine();
			
			// 4 - DESCRIPTION = str
			description = in.readLine();
			
			// 5 - FILE NAME = str
			fileName = in.readLine();
			
			// 6 - MINUTES WATTS
			String s = in.readLine();
			if ( (s.startsWith(columns[0]) == false) || (s.endsWith(columns[1]) == false) )
				return 2;
			
			// 7 - [END COURSE HEADER]
			if ( in.readLine().equals("[END COURSE HEADER]") == false )
				return 2;
		}
		catch ( IOException ioe )
		{
			return 1;
		}
		
		return 0;
	}

	/**
	 * Set the description for the session outlined in the .ERG
	 * file.
	 * 
	 * @param description	the new description
	 */
	public void setDescription( String description )
	{
		this.description = "DESCRIPTION = " + description;
	}

	/**
	 * Set the file name field in the .ERG file.  This field
	 * needs to match exactly the physical name of the .ERG
	 * file in the file system.  For example a file called
	 * <code>at-test.erg</code> should set this field to
	 * <code>at-test.erg</code>.
	 * 
	 * @param fileName		the new file name
	 */
	public void setFileName( String fileName )
	{
		this.fileName = "FILE NAME = " + fileName;
	}

	/**
	 * Set the units used by the session described in the .ERG
	 * file.  This can either be: METRIC or ENGLISH.  Other
	 * values will be ignored.
	 * 
	 * @param units		the new measurement units
	 */
	public void setUnits( String units )
	{
		String s = units.toUpperCase();
		
		if ( (s.equals("METRIC")) || (s.equals("ENGLISH")) )
			this.units = "UNITS = " + s;
	}

	/**
	 * Set the version field for the .ERG file.  Computrainer's Coaching
	 * software v1.6 has .ERG file revisions set to 2.  Eariler revisions
	 * of the .ERG file are not supported.
	 * 
	 * @param version	the new version number
	 */
	public void setVersion( String version )
	{
		this.version = "VERSION = " + version;
	}

	//@Override
	public void write()
	{
		System.out.println(HEADER);
		System.out.println(version);
		System.out.println(units);
		System.out.println(description);
		System.out.println(fileName);
		System.out.println(columns[0] + "\t" + columns[1]);
		System.out.println(FOOTER);
	}

	//@Override
	public boolean write( BufferedWriter out )
	{
		try
		{
			out.write(HEADER);
			out.newLine();
			out.write(version);
			out.newLine();
			out.write(units);
			out.newLine();
			out.write(description);
			out.newLine();
			out.write(fileName);
			out.newLine();
			out.write(columns[0] + " " + columns[1]);
			out.newLine();
			out.write(FOOTER);
			out.newLine();
			out.newLine();
		}
		catch ( IOException ioe )
		{
			return false;
		}

		return true;		
	}
}
